﻿using System;
using System.Collections.Generic;
using System.Linq;
using NHibernate.Linq;
using ServiceStack.Logging;
using g42.Dto;
using g42.Entities;
using g42.MapEntitiesDto;

namespace g42.Repos
{
  public class PersonRepository
  {
    List<Person> _PersonList = new List<Person>();
    ILog log;

    public PersonRepository()
    {
      log = LogManager.GetLogger(GetType());
    }

    public List<Person> Get(PersonList request)
    {
      try
      {
        using (var session = db.sessionFactory.OpenSession())
        {
          using (session.BeginTransaction())
          {
            // Entity-Objekte lesen
            // var Personliste_Ent = session.CreateCriteria(typeof(PersonEnt)).List<PersonEnt>();

            // alle Person die request.Filter im Namen haben per Linq2NHibernate lesen
            var enList = 
              (from ku in session.Query<PersonEnt>()
               where ku.Name.Contains(request.Filter)
               select ku);

            // Entity-Objekte in DTO's transformieren
            _PersonList.Clear();
            foreach (PersonEnt k in enList)
            {
              _PersonList.Add(k.ToPerson());
            }
            // DTO's zurückgeben
            return _PersonList;
          }
        }
      }
      catch (Exception E)
      {
        log.Error("Error PersonRepository.Get(): " + E.Message + System.Environment.NewLine + E.InnerException);
        return null;
      }
    }

    public Person Store(Person dto)
    {
      try
      {
        using (var session = db.sessionFactory.OpenSession())
        {
          using (var transaction = session.BeginTransaction())
          {
            PersonEnt ent = dto.ToPersonEnt();
            session.SaveOrUpdate(ent); // die Id kommt beim Speichern zurück
            transaction.Commit();
            dto.Id = ent.Id;
            return dto;
          }
        }
      }
      catch (Exception E)
      {
        log.Error("Could not store Person: " + E.Message + System.Environment.NewLine + E.InnerException);
        return null;
      }
    }

    public void DeleteByIds(params Guid[] ids)
    {
      _PersonList.RemoveAll(x => ids.Contains(x.Id));
    }

    /*
    public List<Kunde> GetByIds(int[] ids)
    {
      try
      {
        using (var session = db.sessionFactory.OpenSession())
        {
          var makeQuery = (from make in session.Query<KundeEnt>()
                           where make.Id == ids[0]
                           select make).Single();
          _KundeList.Clear();
          _KundeList.Add(new Kunde { Id = makeQuery.Id, Name = makeQuery.Name, VorName = makeQuery.Vorname });
          return _KundeList;
        }
      }
      catch (Exception E)
      {
        MALogger.Error("Could not GetAll Kunde: " + E.Message + System.Environment.NewLine + E.InnerException);
        return null;
      }
    }
    
    public List<Kunde> GetAll()
    {
      try
      {
        using (var session = db.sessionFactory.OpenSession())
        {
          using (session.BeginTransaction())
          {
            var kundenliste_Ent = session.CreateCriteria(typeof(KundeEnt))
                .List<KundeEnt>();
            _KundeList.Clear();
            foreach (KundeEnt k in kundenliste_Ent)
            {
              _KundeList.Add(new Kunde { Id = k.Id, Name = k.Name, VorName = k.Vorname });
            }
            return _KundeList;
          }
        }
      }
      catch (Exception E)
      {
        MALogger.Error("Could not GetAll Kunde: " + E.Message + System.Environment.NewLine + E.InnerException);
        return null;
      }
    }
     */

  }
}
